package services

import (
	"errors"
	"github.com/gin-gonic/gin"
	"hitme/go-api/logic/form"
	"hitme/go-api/logic/repository"
	"hitme/go-api/modules/api"
	"strings"
)

type photoSpecService struct {}

var PhotoSpecService = new(photoSpecService)

// GetList 获取列表
func (u *photoSpecService) GetList (c *gin.Context, req form.PhotoSpecGetListReqForm) (*form.PhotoSpecGetListRespForm, error) {
	req.FillDefaultPage()

	// 获取规格列表
	data, total := repository.PhotoSpecRep.GetList(req.IsHot, req.SpecName, "", req.PerPage, req.GetOffset())

	return &form.PhotoSpecGetListRespForm{
		PaginationResponse: req.ToResponse(total),
		Row:                data,
	}, nil
}

// GetCateList 获取分类列表
func (u *photoSpecService) GetCateList (c *gin.Context, req form.PhotoSpecGetCateListReqForm) (*form.PhotoSpecGetCateListRespForm, error) {
	// 获取分类列表
	cateList := repository.PhotoSpecRep.GetCateList()

	var res []form.PhotoSpecGetCateListReqItem
	for _, val := range cateList {
		// 获取规格列表
		data, _ := repository.PhotoSpecRep.GetList(0, "", val,10, 0)
		res = append(res, form.PhotoSpecGetCateListReqItem{
			CateName: val,
			Datas: data,
		})
	}

	return &form.PhotoSpecGetCateListRespForm{
		Row: res,
	}, nil
}

// GetListByCate 按照分类获取列表
func (u *photoSpecService) GetListByCate (c *gin.Context, req form.PhotoSpecGetListByCateReqForm) (*form.PhotoSpecGetListByCateRespForm, error) {
	// 获取规格列表
	data, _ := repository.PhotoSpecRep.GetList(0, "", req.CateName,1000, 0)

	return &form.PhotoSpecGetListByCateRespForm{
		Row: data,
	}, nil
}

func (u *photoSpecService) getBackGroundColor(color string) api.PhotoSpecBackgroundColor {
	var item api.PhotoSpecBackgroundColor
	switch color {
		case "red":
			item.StartColor = 15283235
			item.ColorName = "red"
			item.EncColor = 15283235
			break;
		case "white":
			item.StartColor = 16777215
			item.ColorName = "white"
			item.EncColor = 16777215
			break;
		default:
			item.StartColor = 5672150
			item.ColorName = "blue"
			item.EncColor = 5672150
	}

	return item
}

// CutCheckPic 制作并检测证件照
func (u *photoSpecService) CutCheckPic (c *gin.Context, req form.PhotoSpecCutCheckPicReqForm) (*form.PhotoSpecCutCheckPicRespForm, error) {
	// 获取图片规格信息
	info := repository.PhotoSpecRep.FindById(req.Id)
	if info.Id == 0 {
		return nil, errors.New("图片规格不存在")
	}

	// 字段分割
	specPxs := strings.Fields(info.SpecPx)
	specSizes := strings.Fields(info.SpecSize)
	var specPxWidth, specPxHeight, specSizeWidth, specSizeHeight string
	if len(specPxs) > 0 {
		specPxArr := strings.Split(specPxs[0], "x")
		specPxWidth = specPxArr[0]
		specPxHeight = specPxArr[1]
	}
	if len(specSizes) > 0 {
		specSizeArr := strings.Split(specSizes[0], "x")
		specSizeWidth = specSizeArr[0]
		specSizeHeight = specSizeArr[1]
	}

	backgroundColor := []api.PhotoSpecBackgroundColor{
		u.getBackGroundColor(req.Color),
	}

	// 制作并检测证件照
	res, err := api.PhotoSpecApi.CutCheckPic(api.PhotoSpecCutCheckPicRequest{
		SpecId:          info.SpecId,
		File:            req.File,
		IsFair:          req.IsFair,
		FairLevel:       req.FairLevel,
		BackgroundColor: backgroundColor,
	});
	if err != nil {
		return nil, err
	}
	if res.Result.Check != 1 {
		return &form.PhotoSpecCutCheckPicRespForm{
			Check: 0,
			CheckRes: res.NotCheckResults[0].ParamMessage,
			SpecPxWidth: specPxWidth,
			SpecPxHeight: specPxHeight,
			SpecSizeWidth: specSizeWidth,
			SpecSizeHeight: specSizeHeight,
		}, nil
	}
	photoWm := res.Result.FileNameWm[0]
	photoName := res.Result.FileName[0]
	var printWmUrl string
	if len(res.Result.FileNamePrintWm) > 0 {
		printWmUrl = api.PhotoSpecApi.TakePicWm(res.Result.FileNamePrintWm[0])
	}

	return &form.PhotoSpecCutCheckPicRespForm{
		Check:          1,
		PhotoWmUrl:     api.PhotoSpecApi.TakePicWm(photoWm),
		PrintWmUrl:     printWmUrl,
		PhotoName:      photoName,
		SpecPxWidth:    specPxWidth,
		SpecPxHeight:   specPxHeight,
		SpecSizeWidth:  specSizeWidth,
		SpecSizeHeight: specSizeHeight,
	}, nil
}

// CutChangeClothes 剪裁换正装
func (u *photoSpecService) CutChangeClothes (c *gin.Context, req form.PhotoSpecCutChangeClothesReqForm) (*form.PhotoSpecCutChangeClothesRespForm, error) {
	// 获取图片规格信息
	info := repository.PhotoSpecRep.FindById(req.Id)
	if info.Id == 0 {
		return nil, errors.New("图片规格不存在")
	}

	// 字段分割
	specPxs := strings.Fields(info.SpecPx)
	specSizes := strings.Fields(info.SpecSize)
	var specPxWidth, specPxHeight, specSizeWidth, specSizeHeight string
	if len(specPxs) > 0 {
		specPxArr := strings.Split(specPxs[0], "x")
		specPxWidth = specPxArr[0]
		specPxHeight = specPxArr[1]
	}
	if len(specSizes) > 0 {
		specSizeArr := strings.Split(specSizes[0], "x")
		specSizeWidth = specSizeArr[0]
		specSizeHeight = specSizeArr[1]
	}

	backgroundColor := []api.PhotoSpecBackgroundColor{
		u.getBackGroundColor(req.Color),
	}

	// 剪裁换正装
	res, err := api.PhotoSpecApi.CutChangeClothes(api.PhotoSpecCutChangeClothesRequest{
		SpecId:          info.SpecId,
		File:            req.File,
		FairLevel:       req.FairLevel,
		BackgroundColor: backgroundColor,
		Clothes:         req.Clothes,
	});
	if err != nil {
		return nil, err
	}
	photoWm := res.WmPicUrl[0]
	photoName := res.FinalPicName[0]
	var printWm string
	if len(res.PrintWmPicUrl) > 0 {
		printWm = res.PrintWmPicUrl[0]
	}

	return &form.PhotoSpecCutChangeClothesRespForm{
		PhotoWmUrl:     photoWm,
		PrintWmUrl:     printWm,
		PhotoName:      photoName,
		SpecPxWidth:    specPxWidth,
		SpecPxHeight:   specPxHeight,
		SpecSizeWidth:  specSizeWidth,
		SpecSizeHeight: specSizeHeight,
	}, nil
}

// EnvPic 检测证件照
func (u *photoSpecService) EnvPic (c *gin.Context, req form.PhotoSpecEnvPicReqForm) (*form.PhotoSpecEnvPicRespForm, error) {
	// 制作并检测证件照
	res, err := api.PhotoSpecApi.EnvPic(api.PhotoSpecEnvPicRequest{
		File: req.File,
	});
	if err != nil {
		return nil, errors.New("证件照环境检测异常")
	}

	if res.Result.Facepose == 0 {
		return &form.PhotoSpecEnvPicRespForm{
			Check: 0,
			Res: "人脸未正对镜头",
		}, nil
	}
	if res.Result.Eyegaze == 0 {
		return &form.PhotoSpecEnvPicRespForm{
			Check: 0,
			Res: "视线不正常",
		}, nil
	}
	if res.Result.Eyeskew == 0 {
		return &form.PhotoSpecEnvPicRespForm{
			Check: 0,
			Res: "眼睛歪斜",
		}, nil
	}
	if res.Result.Shoulderskew == 0 {
		return &form.PhotoSpecEnvPicRespForm{
			Check: 0,
			Res: "肩膀未水平正对镜头",
		}, nil
	}
	if res.Result.Darkillum == 0 {
		return &form.PhotoSpecEnvPicRespForm{
			Check: 0,
			Res: "照片光照昏暗",
		}, nil
	}
	if res.Result.Bfsimilarity == 0 {
		return &form.PhotoSpecEnvPicRespForm{
			Check: 0,
			Res: "衣服和背景颜色相似",
		}, nil
	}

	return &form.PhotoSpecEnvPicRespForm{
		Check: 1,
		Res: "检测通过",
	}, nil
}